package arithmetic.Singleton;

/**
 * Created by jiangfeng on 2019-7-13
 *
 * double checke 懒汉单例
 */
public class Singleton1 {
    private static volatile Singleton1 instance;
    private Singleton1() {
        System.out.println(Thread.currentThread().getName() + " initial!");
    }

    public static Singleton1 getInstance() {
        if (instance == null) {
            synchronized (Singleton1.class) {
                if (instance == null) {
                    instance = new Singleton1();
                }
            }
        }
        return instance;
    }

    /**
     * 反例
     * @return
     */
    public static Singleton1 getInstance2() {
        if (instance == null) {
            instance = new Singleton1();
        }
        return instance;
    }
}
